package control
{
   import flash.display.GradientType;
   import flash.display.MovieClip;
   import flash.events.MouseEvent;
   import flash.filters.DropShadowFilter;
   import flash.geom.Matrix;
   import flash.text.TextField;
   import flash.text.TextFieldAutoSize;
   import flash.text.TextFieldType;
   import flash.text.TextFormat;
   
   public class RomantaButton extends MovieClip
   {
      private const _HEIGHT: Number = 18
      private const _WIDTH: Number = 100
      private var _text:TextField = new TextField
      private var reactArea: MovieClip = new MovieClip
      
      public function RomantaButton(caption:String = "", w:Number=_WIDTH, h:Number=_HEIGHT)
      {
         super()
         // text field
         _text.width = w
         _text.height = h
         _text.selectable = false
         _text.defaultTextFormat = new TextFormat("Arial", 10, 0xFFFFFF, true)
         _text.type = TextFieldType.DYNAMIC
         _text.autoSize = TextFieldAutoSize.CENTER
         _text.multiline = false
         _text.text = caption
         _text.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown)
         addChild(_text)
         // add event listener
         drawUpButton()
         drawReactArea()
      }

      public function set caption (value:String): void {
         _text.text = value
      }

      public function get caption (): String {
         return _text.text
      }

      private function drawUpButton (): void {
         graphics.clear()
         graphics.beginFill(0x666666, 0.8)
         graphics.drawRoundRect(0, 0, _WIDTH, _HEIGHT, _HEIGHT, _HEIGHT)
         graphics.endFill()
      }

      private function drawOverButton (): void {
         var matr: Matrix = new Matrix()
         matr.createGradientBox(100, 40, Math.PI/2, -25, -25)
         graphics.clear()
         graphics.beginGradientFill(GradientType.LINEAR, [0x00CBFF, 0x006699], [1.0, 1.0], [100, 255], matr)
         graphics.drawRoundRect(0, 0, _WIDTH, _HEIGHT, _HEIGHT, _HEIGHT)
         graphics.endFill()         
      }
      
      private function drawReactArea (): void {
         reactArea.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown)
         reactArea.addEventListener(MouseEvent.MOUSE_UP, onMouseUp)
         reactArea.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver)
         reactArea.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut)

         reactArea.buttonMode = true
         reactArea.useHandCursor = true
         reactArea.visible = true
         reactArea.graphics.beginFill(0xFF0000, 0.0)
         reactArea.graphics.drawRoundRect(0, 0, _WIDTH, _HEIGHT, _HEIGHT, _HEIGHT)
         reactArea.graphics.endFill()
         addChild(reactArea)    
      }

      private function onMouseDown (event:MouseEvent): void {
         y += 1
         drawOverButton()
         filters = [new DropShadowFilter(0, 90, 0x000000, 0.7, 4, 4)]
      }

      private function onMouseUp (event:MouseEvent): void {
         y -= 1
         drawOverButton()
         filters = [new DropShadowFilter(2, 90, 0x000000, 0.7, 4, 4)]         
      }

      private function onMouseOver (event:MouseEvent): void {
         drawOverButton()
         filters = [new DropShadowFilter(2, 90, 0x000000, 0.7, 4, 4)]
      }

      private function onMouseOut (event:MouseEvent): void {
         drawUpButton()
         filters = []
      }

   }
}